package com.itlong.cloud.utils.words;

import com.itlong.cloud.POJO.DTO.common.AccessToken;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信accessToken工具类
 *
 * @author Jiaqi.X
 * @version 1.0
 * @date 2016-12-21 14:50:24
 */
public class AccessTokenUtil {
    protected static final Logger LOG = LoggerFactory.getLogger(AccessTokenUtil.class);
    /**
     * 获取accessToken
     * @param appID 微信公众号凭证
     * @param appScret  微信公众号凭证秘钥
     * @return
     */
    public static AccessToken getAccessToken(String appID, String appScret){
        AccessToken token = new AccessToken();
        String message = null;
        try {
            message = doGet("https://api.weixin.qq.com/cgi-bin/token",appID, appScret);
            LOG.info("【accessToken】1：",message);
            token = JsonUtil.toObject(message, AccessToken.class);
            LOG.info("【token】2：",JsonUtil.toJSON(token));
            token.setExpires_in(120*60);
        } catch (MalformedURLException e) {
            LOG.error("【accessToken】3：", message, e);
        } catch (IOException e) {
            LOG.error("【accessToken】4：", message,e);
        } catch (Exception e) {
            LOG.error("【accessToken】5：", message,e);
        }
        return token;
    }

    /**
     * <desc>
     *      doget
     * </desc>
     *
     * @author Jiaqi.X
     * @createDate 2019/05/30
     */
    public static String doGet(String url,String appid,String secret) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建参数队列
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("appid",appid));
        params.add(new BasicNameValuePair("secret", secret));
        params.add(new BasicNameValuePair("grant_type","client_credential"));
        String result = null;
        try {
            //参数转换为字符串
            String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
            url += "?" + paramsStr;
            // 创建httpget.
            HttpGet httpget = new HttpGet(url);
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
                    System.out.println("Response content length: " + entity.getContentLength());
                    // 打印响应内容
                    result= EntityUtils.toString(entity);
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
